package marchsoft.modules.tiku.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import marchsoft.annotation.rest.AnonymousDeleteMapping;
import marchsoft.annotation.rest.AnonymousGetMapping;
import marchsoft.annotation.rest.AnonymousPostMapping;
import marchsoft.annotation.rest.AnonymousPutMapping;
import marchsoft.base.PageVO;
import marchsoft.enums.ResultEnum;
import marchsoft.exception.BadRequestException;
import marchsoft.modules.tiku.entity.dto.EStudyDataDto;
import marchsoft.modules.tiku.entity.dto.EStudyDataTag;
import marchsoft.modules.tiku.entity.dto.StudyDataQueryCriteria;
import marchsoft.response.Result;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;

import marchsoft.modules.tiku.service.IEStudyDataService;
import marchsoft.modules.tiku.entity.EStudyData;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Set;


/**
* <p>
* 学习资料表 前端控制器
* </p>
* @author zsy
* @since 2022-03-29
*/
@RequiredArgsConstructor
@RestController
@Slf4j
@Api(tags = "学习资料表模块")
@CacheConfig(cacheNames = "studyData")
@RequestMapping("/api/eStudyData")
public class EStudyDataController {
    private final IEStudyDataService eStudyDataService;

    @ApiOperation(value = "前台：分页查询某个安全类别的学习资料",notes = "\n anther: zsy")
    @AnonymousGetMapping("/front-all")
    public Result<Object> queryES(Long safeTypeId,String title,PageVO pageVO){
        pageVO.setOrders("[{'column': 'create_time','asc': false}]");
        StudyDataQueryCriteria criteria=new StudyDataQueryCriteria();
        criteria.setKeywords(title);
        criteria.setSafeTypeId(safeTypeId);
        return Result.success(eStudyDataService.queryAllByEs(criteria,pageVO));
    }
    @AnonymousGetMapping(value = "/front")
    public Result<Object> getFrontById(Long id){
        EStudyData eStudyData=eStudyDataService.getById(id);
        return Result.success(eStudyData);
    }
    @AnonymousPostMapping(value = "/front/like")
    public void updateLike(@RequestBody EStudyDataTag studyDataTag){
        EStudyData eStudyData = new EStudyData();

        eStudyData.setId(studyDataTag.getId());
        eStudyData.setTags(studyDataTag.getCount());
        eStudyDataService.updateById(eStudyData);
    }
    @AnonymousPostMapping(value = "/front/dislike")
    public void updateDisLike(@RequestBody EStudyDataTag studyDataTag){
        EStudyData eStudyData=new EStudyData();
        eStudyData.setId(studyDataTag.getId());
        eStudyData.setDislike(studyDataTag.getCount());
        eStudyDataService.updateById(eStudyData);
    }
    @AnonymousPostMapping(value = "/front/read")
    public void updataView(@RequestBody EStudyDataTag eStudyDataTag){
        EStudyData eStudyData=new EStudyData();
        eStudyData.setId(eStudyDataTag.getId());
        eStudyData.setPageView(eStudyDataTag.getCount());
        eStudyDataService.updateById(eStudyData);
    }

//    @AnonymousGetMapping(value = "/front-all")
//    public Result<IPage<EStudyData>> getFrontAll(PageVO pageVO ,EStudyData eStudyData){
//        IPage<EStudyData> eStudyData1= eStudyDataService.getFront(pageVO,eStudyData);
//        return Result.success(eStudyData1);
//    }
    @AnonymousGetMapping(value = "/front/daily")
    public Result<Object> getFrontDaily(PageVO pageVO){
        IPage<EStudyData> all=eStudyDataService.page(pageVO.buildPage());
        return Result.success(all);
    }

    @ApiOperation("导出用户数据")
    @GetMapping(value = "/download")
    public void download(HttpServletResponse response, EStudyDataDto eStudyDataDto) throws IOException {
        //每页条数设置为-1，是查询全部
        PageVO pageVO = new PageVO();
        pageVO.setSize(-1);
        Result<IPage<EStudyDataDto>> pageResult = getAll(eStudyDataDto, pageVO);
        List<EStudyDataDto> eStudyDataDtoList= pageResult.getData().getRecords();
        eStudyDataService.download(eStudyDataDtoList, response);
    }
    @ApiOperation("查询学习资料")
    @GetMapping
//    @Cacheable(key = "'all:'+#pageVO.current")
    public Result<IPage<EStudyDataDto>> getAll(EStudyDataDto eStudyDataDto,PageVO pageVO){
        IPage<EStudyDataDto> all=eStudyDataService.getAll(eStudyDataDto,pageVO);
//        IPage<EStudyData> all=eStudyDataService.page(pageVO.buildPage());

        return Result.success(all);
    }
    @ApiOperation("删除学习资料")
    @DeleteMapping
    @CacheEvict(key = "'all:'",allEntries = true)
    public  Result<Object> deleteStudy(@RequestBody Set<Long> ids){
        if(CollectionUtil.isEmpty(ids)){
            throw new BadRequestException(ResultEnum. PARAM_IS_BLANK);
        }
        eStudyDataService.removeByIds(ids);
        return Result.success();
    }
    @ApiOperation("修改学习资料")
    @PutMapping
    @CacheEvict(key = "'all:'",allEntries = true)
    public Result<Object> updateStudy(@RequestBody @Validated EStudyData eStudyData){
        if(ObjectUtil.isEmpty(eStudyData.getId())){
            throw new BadRequestException(ResultEnum. PARAM_IS_BLANK);
        }
        eStudyDataService.updateById(eStudyData);
        return Result.success();
    }
    @ApiOperation("新增学习资料")
    @PostMapping
    @CacheEvict(key = "'all:'",allEntries = true)
    public Result<Object> addStudy(@RequestBody @Validated EStudyData eStudyData){
        eStudyDataService.save(eStudyData);
        return Result.success();
    }
    @ApiOperation("是否推荐")
    @PutMapping("/changeIntroduce")
    @CacheEvict(key = "'all:'",allEntries = true)
    public Result<Object> updateIntro(@RequestBody EStudyData eStudyData){
        if(ObjectUtil.isEmpty(eStudyData.getId())){
            throw new BadRequestException(ResultEnum. PARAM_IS_BLANK);
        }
        eStudyDataService.updateIntro(eStudyData);
        return Result.success();
    }


}
